package com.lxp.userms.controller;

import com.lxp.userms.entity.*;
import com.lxp.userms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/expert")
public class ExpertController {

    @Autowired
    private ExpertAnswerService answerService;

    @Autowired
    private ExpertLectureService lectureService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    // 用户提问
    @GetMapping("/ask")
    public String showAskQuestionPage(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        // 获取所有专家用户
        List<User> experts = userService.selectUsersByRole(1); // 假设专家用户的角色值为1
        // 获取所有分类
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("experts", experts);
        model.addAttribute("categories", categories);
        return "ask_question";
    }

    @PostMapping("/ask")
    public String askQuestion(Question question, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            question.setUserId(user.getId());
            questionService.publishQuestion(question);
            return "redirect:/expert/questions";
        } else {
            return "redirect:/user/login";
        }
    }

    // 查看所有问题
    @GetMapping("/questions")
    public String viewAllQuestions(Model model) {
        List<Question> questions = questionService.getAllQuestions();
        model.addAttribute("questions", questions);
        model.addAttribute("answerService", answerService);
        return "expert_questions";
    }

    // 用户查看自己的问题
    @GetMapping("/my-questions")
    public String viewMyQuestions(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<Question> questions = questionService.getQuestionsByUserId(user.getId());
            model.addAttribute("questions", questions);
            return "my_expert_questions";
        } else {
            return "redirect:/user/login";
        }
    }

    // 专家回复问题
    @PostMapping("/reply/{questionId}")
    public String replyToQuestion(
            @PathVariable int questionId,
            @RequestParam String answerContent,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        if (user != null && user.getRole() == 1) { // 确保只有专家可以回复
            ExpertAnswer answer = new ExpertAnswer();
            answer.setQuestionId(questionId);
            answer.setAnswer(answerContent);
            answer.setAnswerTime(new Date());
            answerService.insertNewAnswer(answer);
        }
        return "redirect:/expert/questions";
    }

    // 专家发布讲座
    @GetMapping("/lecture/publish")
    public String showPublishForm() {
        return "publish_lecture"; // 显示表单
    }

    @PostMapping("/lecture/publish")
    public String doPublish(
            @RequestParam String title,
            @RequestParam String content, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ExpertLecture lecture = new ExpertLecture();
        lecture.setTitle(title);
        lecture.setContent(content);
        lecture.setCreateTime(new Date());
        lecture.setExpertId(user.getId());
        lectureService.publishLecture(lecture);
        return "redirect:/expert/lectures";
    }


    // 查看所有讲座
    @GetMapping("/lectures")
    public String viewAllLectures(Model model) {
        List<ExpertLecture> lectures = lectureService.getAllLectures();
        model.addAttribute("lectures", lectures);
        return "expert_lectures";
    }

    // 专家查看自己的讲座
    @GetMapping("/my-lectures")
    public String viewMyLectures(Model model, HttpSession session) {
        // 从会话中获取当前专家ID
        User user = (User) session.getAttribute("user");
        System.out.println("Current expertId in session: " + session.getAttribute("expertId"));

        if (user == null) {
            // 如果未登录或会话失效，跳转至登录页或其他处理方式
            return "redirect:/keshe/login";
        }
        List<ExpertLecture> lectures = lectureService.getLecturesByExpertId(user.getId());
        model.addAttribute("lectures", lectures);
        return "my_expert_lectures";
    }

    // 删除讲座
    @GetMapping("/lecture/delete/{id}")
    public String deleteLecture(@PathVariable int id, HttpSession session) {

        lectureService.deleteLecture(id);
        return "redirect:/expert/my-lectures";
    }
}